package gam.n1.be.impl;

import gam.n1.areas.Area;
import gam.n1.areas.impl.SkillPlayBukkitArea;
import gam.n1.be.b.AsynBehavioral;
import gam.n1.skills.Skill;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SkillHandler extends AsynBehavioral {
    private List<Skill> auto;
    private final Map<String, Predicate<Area>> conditions;

    public SkillHandler(Area area) {
        super(area);
        this.conditions = new HashMap<>();
        if (isSkillArea()){
            this.auto = ((SkillPlayBukkitArea) g).getSkills().stream().filter(Skill::isAuto).collect(Collectors.toList());
        }
    }

    public void addAutoSkillRule(Skill skill,Predicate<Area> c){
        conditions.put(skill.name(),c);
    }
    public void setAuto(List<Skill> auto) {
        if (isSkillArea() && !g.isRunning()){
            this.auto = auto;
        }
    }

    private boolean isSkillArea(){
        return g instanceof SkillPlayBukkitArea;
    }

    @Override
    protected void aP() {
        super.aP();
        if (isSkillArea() && auto != null && !auto.isEmpty()){
            auto.forEach(skill -> {
                Predicate<Area> var1 = conditions.get(skill.name());
                if (var1 == null) var1 = some -> true;
                if (var1.test(g)){
                    addTask(BehavioralTask.build(skill::tick,true,var1));
                }
            });
        }
    }
}
